Imports DaveSexton.DocProject.DocSites

Partial Public Class DocSiteSidebar
	Inherits System.Web.UI.UserControl

	<ComponentModel.Category("Appearance"), ComponentModel.DefaultValue(True)> _
	Public Property TableOfContentsSelected() As Boolean
		Get
			Dim value As Nullable(Of Boolean) = CType(ViewState("_$ContentsSelected"), Nullable(Of Boolean))

			Return value.GetValueOrDefault(True)
		End Get
		Set(ByVal value As Boolean)
			ViewState("_$ContentsSelected") = value
		End Set
	End Property

	<ComponentModel.Browsable(False)> _
	Public ReadOnly Property TableOfContents() As DocSiteContents
		Get
			Return _TableOfContents
		End Get
	End Property

	<ComponentModel.Browsable(False)> _
	Public ReadOnly Property Index() As DocSiteIndex
		Get
			Return _Index
		End Get
	End Property

	Private _changingSelection As Boolean

	Public Sub Initialize(ByVal topic As String, ByVal helpFile As String)
		_changingSelection = True

		If helpFile Is Nothing Then
			helpFile = String.Empty
		Else
			helpFile = helpFile.ToLowerInvariant()
		End If

		_TableOfContents.SelectedTopic = topic
		_Index.SelectedHelpFile = helpFile

		_changingSelection = False
	End Sub

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Page.IsPostBack Then
			TableOfContentsSelected = Not "sidebar_index_button".Equals(selectedButtonHiddenField.Value, StringComparison.OrdinalIgnoreCase)
		End If
	End Sub

	Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
		Dim button As String = "sidebar_index_button"
		Dim element As String = "docsite_index"

		If TableOfContentsSelected Then
			button = "sidebar_contents_button"
			element = "docsite_toc"
		End If

		Dim script As String = String.Format( _
		 "selectDocsiteSidebarButton(document.getElementById(""{0}""), document.getElementById(""{1}""));", button, element)

		Page.ClientScript.RegisterStartupScript(GetType(DocSiteSidebar), "initSidebar", script, True)

		MyBase.OnPreRender(e)
	End Sub

	Protected Sub contents_SelectedTopicChanged(ByVal sender As Object, ByVal e As EventArgs)
		If Not _changingSelection Then
			_changingSelection = True
			TableOfContentsSelected = True

			If (DocSiteNavigator.NavigateToTopic(_TableOfContents.SelectedTopic, False)) Then
				_Index.SelectedHelpFile = DocSiteNavigator.ResolveTopicHelpFile(_TableOfContents.SelectedTopic, False)
			End If

			_changingSelection = False
		End If
	End Sub

	Protected Sub index_SelectedHelpFileChanged(ByVal sender As Object, ByVal e As EventArgs)
		If Not _changingSelection Then
			_changingSelection = True
			TableOfContentsSelected = False

			DocSiteNavigator.NavigateToHelpFile(_Index.SelectedHelpFile)

			_TableOfContents.SelectedTopic = DocSiteNavigator.ResolveHelpFileTopic(_Index.SelectedHelpFile, False)

			_changingSelection = False
		End If
	End Sub

End Class